//
//  ProductDetailViewController.m
//  Castlery
//
//  Created by xie weizhi on 8/8/17.
//  Copyright © 2017 xie weizhi. All rights reserved.
//

#import "ProductDetailViewController.h"
// Views
#import "ImageListView.h"
#import "ProductDetailInfoView.h"
// Models
#import "ProductDetailDataModel.h"

@interface ProductDetailViewController () <ImageListViewDelegate>

@property (nonatomic, strong) ProductDetailInfoView *detailInfoView;
@property (nonatomic, strong) ImageListView *imageListView;
@property (nonatomic, strong) UILabel *descriptionLabel;
@property (nonatomic, strong) UIScrollView *contentScrollView;
@property (nonatomic, strong) UIActivityIndicatorView *loadingIndicator;

@property (nonatomic, strong) ProductDetailDataModel *dataModel;
@property (nonatomic, strong) Product *product;

@end

@implementation ProductDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self fetchData];
}

#pragma mark - Private

- (void)fetchData {
    if (!self.loadingIndicator.superview) {
        [self.contentScrollView addSubview:self.loadingIndicator];
        self.loadingIndicator.center = self.view.center;
    }
    [self.loadingIndicator startAnimating];
    
    
    __weak typeof(self) weakSelf = self;
    [self.dataModel requestProductWithProductID:self.productID completion:^(NSError *error, Product *product) {

        __strong typeof(weakSelf) strongSelf = weakSelf;

        [strongSelf.loadingIndicator stopAnimating];
        
        if (error) {
            [strongSelf showRetryAlert];
            return;
        }

        strongSelf.product = product;
        [strongSelf setupContentView];
    }];
}

- (void)showRetryAlert {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Error" message:@"Request failed. Try Again?" preferredStyle:UIAlertControllerStyleAlert];
    
    __weak typeof(self) weakSelf = self;
    [alertController addAction:[UIAlertAction actionWithTitle:@"NO" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }]];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"Retry" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf fetchData];
    }]];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)setupContentView {
    [self.view addSubview:self.contentScrollView];
    self.contentScrollView.contentSize = self.view.bounds.size;
    [NSLayoutConstraint
     activateConstraints:@[[self.contentScrollView.topAnchor constraintEqualToAnchor:self.view.topAnchor],
                           [self.contentScrollView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor],
                           [self.contentScrollView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor],
                           [self.contentScrollView.heightAnchor constraintEqualToAnchor:self.view.heightAnchor],
                           ]];
    
    UIView *containerView = [UIView new];
    containerView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentScrollView addSubview:containerView];
    [NSLayoutConstraint
     activateConstraints:@[[containerView.topAnchor constraintEqualToAnchor:self.contentScrollView.topAnchor],
                           [containerView.rightAnchor constraintEqualToAnchor:self.contentScrollView.rightAnchor],
                           [containerView.leftAnchor constraintEqualToAnchor:self.contentScrollView.leftAnchor],
                           [containerView.bottomAnchor constraintEqualToAnchor:self.contentScrollView.bottomAnchor],
                           [containerView.heightAnchor constraintEqualToAnchor:self.contentScrollView.heightAnchor],
                           [containerView.widthAnchor constraintEqualToAnchor:self.contentScrollView.widthAnchor],
                           ]];

    [containerView addSubview:self.detailInfoView];
    [NSLayoutConstraint
     activateConstraints:@[[self.detailInfoView.leftAnchor constraintEqualToAnchor:containerView.leftAnchor],
                           [self.detailInfoView.widthAnchor constraintEqualToAnchor:containerView.widthAnchor],
                           [self.detailInfoView.rightAnchor constraintEqualToAnchor:containerView.rightAnchor],
                           [self.detailInfoView.topAnchor constraintEqualToAnchor:containerView.topAnchor],
                           [self.detailInfoView.heightAnchor constraintEqualToConstant:340.f]
                           ]];
    
    UILabel *imageLabel = [UILabel new];
    imageLabel.font = [UIFont systemFontOfSize:14.f];
    imageLabel.text = @"Color";
    imageLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [containerView addSubview:imageLabel];
    [NSLayoutConstraint
     activateConstraints:@[[imageLabel.leftAnchor constraintLessThanOrEqualToAnchor:containerView.leftAnchor
                                                                           constant:20.f],
                           [imageLabel.heightAnchor constraintEqualToConstant:30.f],
                           [imageLabel.topAnchor constraintEqualToAnchor:self.detailInfoView.bottomAnchor
                                                                constant:10.f],]];
    
    [containerView addSubview:self.imageListView];
    [NSLayoutConstraint
     activateConstraints:@[[self.imageListView.leftAnchor constraintEqualToAnchor:containerView.leftAnchor],
                           [self.imageListView.rightAnchor constraintEqualToAnchor:containerView.rightAnchor],
                           
                           [self.imageListView.heightAnchor constraintEqualToConstant:ProductListItemSize],
                           [self.imageListView.topAnchor constraintEqualToAnchor:imageLabel.bottomAnchor],]];
    
    [containerView addSubview:self.descriptionLabel];
    [NSLayoutConstraint
     activateConstraints:@[[self.descriptionLabel.topAnchor constraintEqualToAnchor:self.imageListView.bottomAnchor
                                                                           constant:5.f],
                           [self.descriptionLabel.rightAnchor constraintEqualToAnchor:containerView.rightAnchor
                                                                             constant:-20.f],
                           [self.descriptionLabel.leftAnchor constraintEqualToAnchor:containerView.leftAnchor
                                                                            constant:20.f],
                           [containerView.bottomAnchor constraintEqualToAnchor:self.descriptionLabel.bottomAnchor
                                                                      constant:20.f]]];
    

    
    
    [self.detailInfoView updateViewsWithProduct:_product];
    [self.imageListView reloadWithUrls:[_product imageURLs]];
    
    self.descriptionLabel.text = self.product.productDescription;
}

#pragma mark - UI Actions

#pragma mark - ImageListViewDelegate

- (void)imageList:(ImageListView *)imageListView didSelectImageAtIndex:(NSInteger)index {
    [self.detailInfoView updateImageViewWithURL:self.product.imageURLs[index]];
}

#pragma mark - Accessors

- (ProductDetailInfoView *)detailInfoView {
    if (!_detailInfoView) {
        _detailInfoView = [ProductDetailInfoView new];
        _detailInfoView.translatesAutoresizingMaskIntoConstraints = NO;
    }
    
    return _detailInfoView;
}

- (ImageListView *)imageListView {
    if (!_imageListView) {
        _imageListView = [ImageListView new];
        _imageListView.delegate = self;
        _imageListView.translatesAutoresizingMaskIntoConstraints = NO;
    }
    
    return _imageListView;
}

- (UIScrollView *)contentScrollView {
    if (!_contentScrollView) {
        _contentScrollView = [UIScrollView new];
        _contentScrollView.directionalLockEnabled = YES;
        _contentScrollView.alwaysBounceVertical = YES;
        _contentScrollView.showsVerticalScrollIndicator = NO;
        _contentScrollView.showsHorizontalScrollIndicator = NO;
        _contentScrollView.translatesAutoresizingMaskIntoConstraints = NO;
    }
    
    return _contentScrollView;
}

- (UILabel *)descriptionLabel {
    if (!_descriptionLabel) {
        _descriptionLabel = [UILabel new];
        _descriptionLabel.font = [UIFont systemFontOfSize:12.f];
        _descriptionLabel.numberOfLines = 0;
        _descriptionLabel.translatesAutoresizingMaskIntoConstraints = NO;
    }
    
    return _descriptionLabel;
}

- (ProductDetailDataModel *)dataModel {
    if (!_dataModel) {
        _dataModel = [ProductDetailDataModel new];
    }
    
    return _dataModel;
}

- (UIActivityIndicatorView *)loadingIndicator {
    if (!_loadingIndicator) {
        _loadingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        _loadingIndicator.hidesWhenStopped = YES;
    }
    
    return _loadingIndicator;
}

@end
